/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.base.hibernate.dialect;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.com.ksplatform.base.page.PageInfoModel;

/**
 * ClassName:OracleDialect.java <br/>
 * Date: 2015年11月23日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component("dialects.oracle")
public class OracleDialect implements IDialect{

	@Override
	public String getPageSql(String sql, PageInfoModel page) {
		// 设置分页参数
		int pageSize = page.getPageSize();
		int pageNo = page.getPage();
		StringBuffer sb = new StringBuffer();
				
		sb.append("SELECT * FROM ( SELECT ROWNUM RN ,A_.*  FROM (").append(sql).append( 
		        ") A_ ) WHERE RN>").append(
		        pageSize * (pageNo - 1)).append(" AND RN<=").append(pageSize * pageNo);
		
		if (StringUtils.hasText(page.getSortName())) {
			sb.append(" order by ").append(page.getSortName()).append(" ").append(page.getSortOrder());
		}
		return sb.toString();
	}

	@Override
	public String getCountSql(String sql) {
		String countSql = "select count(*) from ("+sql+") t1";
		return countSql;
	}

}
